package patients

import (
	"errors"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/patients"
	patientsReq "github.com/flipped-aurora/gin-vue-admin/server/model/patients/request"
	"gorm.io/gorm"
)

type PatientService struct{}

// CreatePatient 创建patient表记录
// Author [yourname](https://github.com/yourname)
func (patientService *PatientService) CreatePatient(patient *patients.Patient) (err error) {
	s := patient.PatientImage[len(patient.PatientImage)-4:]
	var map1 = make(map[string]bool)
	map1[".png"] = true
	map1[".jpg"] = true
	map1[".jpeg"] = true
	if !map1[s] {
		return errors.New("上传的图片只能为png或jpg或jpeg类型")
	}
	err = global.GVA_DB.Create(patient).Error
	return err
}

// DeletePatient 删除patient表记录
// Author [yourname](https://github.com/yourname)
func (patientService *PatientService) DeletePatient(ID string, userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&patients.Patient{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
			return err
		}
		if err = tx.Delete(&patients.Patient{}, "id = ?", ID).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// DeletePatientByIds 批量删除patient表记录
// Author [yourname](https://github.com/yourname)
func (patientService *PatientService) DeletePatientByIds(IDs []string, deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&patients.Patient{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
			return err
		}
		if err := tx.Where("id in ?", IDs).Delete(&patients.Patient{}).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// UpdatePatient 更新patient表记录
// Author [yourname](https://github.com/yourname)
func (patientService *PatientService) UpdatePatient(patient patients.Patient) (err error) {
	err = global.GVA_DB.Model(&patients.Patient{}).Where("id = ?", patient.ID).Updates(&patient).Error
	return err
}

// GetPatient 根据ID获取patient表记录
// Author [yourname](https://github.com/yourname)
func (patientService *PatientService) GetPatient(ID string) (patient patients.Patient, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&patient).Error
	return
}

// GetPatientInfoList 分页获取patient表记录
// Author [yourname](https://github.com/yourname)
func (patientService *PatientService) GetPatientInfoList(info patientsReq.PatientSearch) (list []patients.Patient, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&patients.Patient{})
	var patients []patients.Patient
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Name != nil && *info.Name != "" {
		db = db.Where("name LIKE ?", "%"+*info.Name+"%")
	}
	if info.Mobile != nil && *info.Mobile != "" {
		db = db.Where("mobile = ?", *info.Mobile)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&patients).Error
	return patients, total, err
}
func (patientService *PatientService) GetPatientPublic() {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}

func (patientService *PatientService) GetPatientGroups() (patient []patients.Patient) {
	global.GVA_DB.Find(&patient)
	return
}

func (patientService *PatientService) GetDepartmentGroups() (department []patients.Department) {
	global.GVA_DB.Find(&department)
	return
}
